#include <stdio.h>
#include <js/jsapi.h>
#include <string.h>
#include <pwd.h>
#include "monkeyruntime.h"
#include "monkeycontext.h"
#include "PhidgetManager.h"
#include "PhidgetRootObject.h"
#include "PhidgetPropertyAlias.h"
#include "MonkeySystemObject.h"
#include "MessageCenter.h"
#include "Message.h"
#include "WebServer.h"
#include "WebHandler.h"
#include "SystemDatabase.h"
#include "Utils.h"


//
// I have a messaging center in place and working. I may need to find some way to come up
// with a "runloop" type system as well. The catch with a runloop environment is how to
// handle "waiting" for, for example, a message and then still being able to respond to
// a keyboard stroke.
//
// I need to look at the Gnustep runloop implementation and see how it works. A runloop
// would let us deal with multiple input sources. One input source could be the message
// system, one being the "networked" message system and another being timer sources,
// network ports, etc.
//
static const char *js_script =
"function func(name, val) {\r\n" \
"  if (val > 81)\r\n" \
"    phidget.value.led = 1;\r\n" \
"  else if (val < 78)\r\n" \
"    phidget.value.led = 0;\r\n" \
"  system.print('callback called: ' + name + ' = ' + val);\r\n" \
"}\r\n" \
"\r\n" \
"phidget.onvaluechange('phidgetValue_roomtemp', func);\r\n" \
"system.daemon();\r\n" \
"\r\n" \
"while (1) {\n" \
"  phidget.value.led = phidget.value.state;\n" \
"  system.print(phidget.value['roomtemp']);\n" \
"  system.sleep(1000);\n" \
"}";

PhidgetManager *gPhidgetManager = NULL;

int main(int argc, char *argv[])
{
    MonkeyRuntime *rt = MonkeyRuntime::SharedRuntime();
    MonkeyContext *ctx;
    WebServer *webServer = NULL;


    struct passwd *pw = getpwuid(getuid());
    const char *homedir = pw->pw_dir;
    char webRoot[PATH_MAX], database[PATH_MAX];
    
    snprintf(webRoot, PATH_MAX, "%s/Documents/Development/osba/html-static", homedir);
    snprintf(database, PATH_MAX, "%s/Documents/osba.db", homedir);
    webServer = new WebServer(webRoot);
    gSharedDatabase = new SystemDatabase(database);

	//
	// Setup the phidget manager.
	//
    gPhidgetManager = new PhidgetManager(rt, "danielsbc", NULL);
    gPhidgetManager->RegisterPropertyAlias(PhidgetPropertyAlias(48447, "input0", "state"));    
    gPhidgetManager->RegisterPropertyAlias(PhidgetPropertyAlias(48447, "output0", "led"));    
    gPhidgetManager->RegisterPropertyAlias(PhidgetPropertyAlias(48447, "sensor0", "roomtemp", "(value / 10.2375) - 78"));//"(value * 0.4) - 78"));    
    
    //
    // Change to the proper directory.
    //

    //
    // Setup and start the web server.
    //
    RegisterWebHandlers(*webServer);
    webServer->start();
    
	//
	// Give everything a chance to settle.
	//
    sleep(2);

    while (true)
        sleep(1);
	//
	// Process the script.
	//
//    ctx = CreateStandardMonkeyContext();
//    ctx->EvaluateScript(js_script);
    
    delete webServer;
    delete rt;
    delete gPhidgetManager;
    delete gSharedDatabase;
    
    return 0;
}

